# -*- coding:utf8 -*-
__author__ = 'Forcs'

from wanplus.models.base import BaseModel
from django.db import models
import json

class WangwangbaCover(BaseModel):
    """
    封面表
    """
    token = models.CharField(max_length=32, null=False, default='', unique=True)
    relate_name = models.CharField(max_length=512, null=False, default='')
    local_name = models.CharField(max_length=512, null=False, default='')
    remote_name = models.CharField(max_length=512, null=True)
    mime_type = models.CharField(max_length=30, null=True)


class WangwangbaMedia(BaseModel):
    """
    视频表
    """
    token = models.CharField(max_length=32, null=False, default='', unique=True)
    relate_name = models.CharField(max_length=512, null=False, default='')
    local_name = models.CharField(max_length=512, null=False, default='')
    remote_name = models.CharField(max_length=512, null=True)
    mime_type = models.CharField(max_length=30, null=True)


class WangwangbaCategory(BaseModel):
    """
    栏目
    """
    code = models.CharField(max_length=32, null=False, default='', unique=True)
    name = models.CharField(max_length=30, null=False, default='')
    icon_url = models.URLField()
    content_count = models.IntegerField(null=False, default=0)
    is_invalid = models.BooleanField(default=False)


class WangwangbaContent(BaseModel):
    """
    媒体资源
    """
    token = models.CharField(max_length=32, null=False, default='', unique=True)
    content_code = models.CharField(max_length=256, null=False, default='')
    content_name = models.CharField(max_length=256, null=False, default='')
    content_type = models.SmallIntegerField(null=False, default=-1)
    content_state = models.SmallIntegerField(null=False, default=0)
    data = models.TextField(null=False, default='')
    category = models.ForeignKey(WangwangbaCategory, related_name='content_cate', null=True)
    cover_url = models.URLField()
    is_recommend = models.BooleanField(default=False)
    is_invalid = models.BooleanField(default=False)
    date_added = models.DateTimeField(null=True)
    date_last = models.DateTimeField(null=True)


class WangwangbaDogtvCovers(BaseModel):
    """
    DogTV封面
    """
    position = models.IntegerField(null=False, default=0)
    token = models.CharField(max_length=32, null=False, default='', unique=True)
    filename = models.URLField()


class WangwangbaDogtvProgram(BaseModel):
    """
    DogTV节目清单
    """
    token = models.CharField(max_length=32, null=False, default='', unique=True)  # 唯一标识
    name = models.CharField(max_length=100, null=False, default='')  # 节目名称
    url = models.URLField(null=False, default='')  # 节目url
    category = models.PositiveSmallIntegerField(null=False, default=0)  # 节目分类
    is_trailer = models.BooleanField(null=False, default=False)  # true 表示作为trailer
    date_added = models.DateTimeField(null=False, auto_now_add=True)  # 添加时间


class WangwangbaSettings(BaseModel):
    """
    汪汪吧设置项
    """
    name = models.CharField(max_length=20, null=False, default='', unique=True)  # 设置项名称
    val = models.TextField(null=False, default='')  # 设置项值
    val_type = models.CharField(max_length=1, null=False, default='s')  # 值类型

    @staticmethod
    def value_of(name, value=None, value_type='s'):
        setting, created = WangwangbaSettings.objects.get_or_create(name=name)
        if value:
            setting.value(value, value_type)
        else:
            return setting.value()

    def value(self, value=None, value_type='s'):
        if value:
            if isinstance(value, int) or self.val_type == 'i' or value_type == 'i':
                self.val = str(value)
                self.val_type = 'i'
            elif isinstance(value, float) or self.val_type == 'f' or value_type == 'f':
                self.val = str(value)
                self.val_type = 'f'
            elif isinstance(value, bool) or self.val_type == 'b' or value_type == 'b':
                self.val = str(value)
                self.val_type = 'b'
            elif isinstance(value, dict) or self.val_type == 'd' or value_type == 'd':
                self.val = json.dumps(value)
                self.val_type = 'd'
            elif isinstance(value, list) or self.val_type == 'l' or value_type == 'l':
                self.val = ','.join(value)
                self.val_type = 'l'
            else:
                self.val = str(value)
                self.val_type = 's'
            self.save()

        else:
            vt = self.val_type
            if vt == 'i':
                return int(self.val)
            elif vt == 'f':
                return float(self.val)
            elif vt == 'b':
                return bool(self.val)
            elif vt == 'd':
                return json.loads(self.val, encoding='utf8')
            elif vt == 'l':
                return self.val.split(',')
            else:
                return self.val
